package com.wyvoid.core.tag.pager;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Iterator;
import java.net.URLEncoder;

/**
 * 分页标签
 * User: cjp
 * Date: 2007-5-28
 * Time: 9:24:52
 */
public class PagerTag extends TagSupport {
    private int curPage = 1;
    private int totalCount;
    private int pageSize = 10;//显示多少页码
    private int viewCount;//每页显示几条记录
    private boolean useDefaultStyle=true;//是否使用默认样式表,如果不用,则用户需要提供CSS

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getViewCount() {
        return viewCount;
    }

    public void setViewCount(int viewCount) {
        this.viewCount = viewCount;
    }

    public boolean isUseDefaultStyle() {
        return useDefaultStyle;
    }

    public void setUseDefaultStyle(boolean useDefaultStyle) {
        this.useDefaultStyle = useDefaultStyle;
    }

    public int doStartTag() throws JspException {
        if(totalCount<=viewCount){
            return SKIP_BODY;
        }
        String bar="";
        if(useDefaultStyle){
            bar+=getDefaultStyle();
        }
        try {
             bar+= "<div class=\"nav_bar\">\n";
            bar += getNavigatorBar();
            //输出结束标记
            bar += "</div>";
            pageContext.getOut().write(bar);
            return SKIP_BODY;
        }
        catch (IOException ioe) {
            throw new JspException(ioe.getMessage());
        }
    }

    private String getDefaultStyle() {
        return "<style type=\"text/css\">\n"+
                ".nav_bar {\n" +
                "    margin: 1px 0px;\n" +
                "    clear: both;\n" +
                "}\n" +
                "\n" +
                ".nav_bar a {\n" +
                "    float: left;\n" +
                "    padding: 1px 4px;\n" +
                "    font-size: 12px;\n" +
                "    text-decoration: none;\n" +
                "    color: #154BA0;\n" +
                "}\n" +
                "\n" +
                ".nav_bar a:hover {\n" +
                "text-decoration: underline;\n" +
                "}\n" +
                ".nav_input {\n" +
                "    border: 0px;\n" +
                "    padding: 0px;\n" +
                "    width: 40px;\n" +
                "    height: 17px !important;\n" +
                "    height: 15px;\n" +
                "    margin: 0px;\n" +
                "    background: #FFFFFF;\n" +
                "}\n" +
                "\n" +
                ".nav_total {\n" +
                "    background-color: #F5FBFF;\n" +
                "    border: 1px solid #86B9D6;\n" +
                "    border-right: 0px solid #86B9D6;\n" +
                "    font-weight: bold;\n" +
                "}\n" +
                "\n" +
                ".nav_pages {\n" +
                "    background-color: #F5FBFF;\n" +
                "    border: 1px solid #86B9D6;\n" +
                "    margin-right: 1px;\n" +
                "    vertical-align: middle;\n" +
                "    font-weight: bold;\n" +
                "}\n" +
                "\n" +
                ".nav_num {\n" +
                "    background-color: #FFFFFF;\n" +
                "    border: 1px solid #DEDEB8;\n" +
                "    margin-right: 1px;\n" +
                "    vertical-align: middle;\n" +
                "}\n" +
                "\n" +
                "a:hover.nav_num {\n" +
                "    background-color: #F5FBFF;\n" +
                "    border: 1px solid #86B9D6;\n" +
                "    text-decoration: none;\n" +
                "}\n" +
                "\n" +
                ".nav_redirect {\n" +
                "    background-color: #FFFFFF;\n" +
                "    border: 1px solid #DEDEB8;\n" +
                "    margin-right: 1px;\n" +
                "    font-size: 12px !important;\n" +
                "    font-size: 13px;\n" +
                "}\n" +
                "\n" +
                "a:hover.nav_redirect {\n" +
                "    background-color: #F5FBFF;\n" +
                "    border: 1px solid #86B9D6;\n" +
                "    text-decoration: none;\n" +
                "}\n" +
                "\n" +
                ".nav_curpage {\n" +
                "    margin-right: 1px;\n" +
                "    border: 1px solid #DEDEB8;\n" +
                "    vertical-align: middle;\n" +
                "    background-color: #FFFFD9;\n" +
                "    color: #92A05A;\n" +
                "    font-weight: bold;\n" +
                "}"
                +"</style>\n";
    }

    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }

    /**
     * 根据指定类型获得导航条预先设计的导航条
     *
     * @return 返回导航条的HTML代码,若指定类型不存在,返回""
     */
    private String getNavigatorBar() {
        String url = "";
        Map map = pageContext.getRequest().getParameterMap();
        for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
            String key = (String) iter.next();
            String value = ((String []) map.get(key))[0];
            try {
                value = URLEncoder.encode(value, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            if (!key.trim().equalsIgnoreCase("curPage")) {
                if (url.trim().length() == 0) {
                    url = url + key + "=" + value;
                } else {
                    url = url + "&" + key + "=" + value;
                }
            }
        }

        url = "?" + url.trim();

        if (url.endsWith("?")) {
            url = url + "curPage=";
        } else {
            url = url + "&curPage=";
        }
        int totalPage;
        if (totalCount % viewCount == 0) {
            totalPage = totalCount / viewCount;
        } else {
            totalPage = totalCount / viewCount + 1;
        }
        String bar;
        StringBuffer selText = new StringBuffer();
        boolean nextDisabled = false;
        boolean prevDisabled = false;
        if (curPage >= totalPage)
            nextDisabled = true;
        if (curPage <= 1)
            prevDisabled = true;

        int prevOrNextCount;

        if (pageSize % 2 == 0) {
            prevOrNextCount = pageSize / 2;
        } else {
            prevOrNextCount = (pageSize - 1) / 2;
        }

        int firstPage = 1;
        int lastPage = totalPage;
        if (totalPage < curPage + pageSize) {
            lastPage = totalPage;
            firstPage = totalPage-pageSize;
            if(firstPage<1){
                firstPage = 1;
            }
        } else {
            if (curPage - prevOrNextCount > 0) {
                firstPage = curPage - prevOrNextCount;
            }
            if (firstPage + prevOrNextCount * 2 < totalPage) {
                lastPage = firstPage + prevOrNextCount * 2;
            }
        }

        String firstText = ""; //首  页
        String prevText = "";  //上一页
        String nextText = "";  //下一页
        String lastText = "";  //最后一页
        String pagesText="";//中间的页码
        if (!prevDisabled) {
            firstText = "<a href=\""+url+"1\" class=\"nav_redirect\">|&lsaquo;</a>";
            prevText = "<a href=\""+url+ (curPage - 1) + "\" class=\"nav_redirect\">&lsaquo;&lsaquo;</a>\n";
        }
        if (!nextDisabled) {
            nextText = "<a href=\""+url + (curPage + 1) + "\" class=\"nav_redirect\">&rsaquo;&rsaquo;</a>";
            lastText = "<a href=\""+url + totalPage + "\" class=\"nav_redirect\">&rsaquo;|</a>\n";
        }
        for(int i=firstPage;i<=lastPage;i++){
            if(curPage==i){
                pagesText+="<a class=\"nav_curpage\">"+i+"</a>";
            }else{
                pagesText+="<a href=\""+url+i+"\" class=\"nav_num\">"+i+"</a>\n";
            }
        }
        selText.append("<a class=\"nav_pages\" style=\"padding: 0px\">" +
                "<input class=\"nav_input\" type=\"text\" name=\"topage\" " +
                "onKeyDown=\"if(event.keyCode==13) " + "{window.location='").
                append(url).append("'+this.value; return false;}\"></a>\n");

        bar = "<a class=\"nav_total\">&nbsp;" + totalCount + "&nbsp;</a>\n" +
              "<a class=\"nav_pages\">&nbsp;" + curPage + "/" + totalPage + "&nbsp;</a>"
              + firstText + " \n " + prevText + " \n " +pagesText+" \n "+ nextText + " \n " + lastText + " \n" + selText.toString() + "\n";

        return bar;
    }
}
